package com.aptemo.webetek.client.widgets;

import java.util.Date;

import com.google.gwt.dom.client.Document;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;

public class DateTimeWidget
		extends Widget implements HasValue<Date>
{

	private DateTimeFormat	formatter;

	public DateTimeWidget()
	{
		formatter = DateTimeFormat.getMediumDateTimeFormat();
		// "dd MMMM yyyy, HH:mm"
		setElement(Document.get().createDivElement());
	}

	public DateTimeWidget(DateTimeFormat formatter)
	{
		this.formatter = formatter;
		setElement(Document.get().createDivElement());
	}

	public DateTimeWidget(String pattern)
	{
		setElement(Document.get().createDivElement());
		formatter = DateTimeFormat.getFormat(pattern);
	}

	@Override
	public void setValue(Date value)
	{
		if (value == null)
		{
			getElement().setInnerText("...");
		}
		else
		{
			getElement().setInnerText(formatter.format(value));
		}
	}

	@Override
	public void setValue(Date value, boolean fireEvents)
	{
		setValue(value);
	}

	@Override
	public Date getValue()
	{
		return formatter.parse(getElement().getInnerText());
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler)
	{
		return null;
	}

}
